package www.openteam.com.model.business;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.InsertStatus;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by kylin on 2016/10/27.
 */

public class InsertProductInfoModel extends BaseModel {

    boolean isInsert = false;

    public boolean insertProductInfo(String code, String name,
                                     String type, String ccode,
                                     String length, String width,
                                     String height, String unit,
                                     String maxstock, String typename,
                                     String CoverPage, String contentpage, String minstock
            , final InsertProductOnLinstener insertProductOnLinstener) {


        httpApiService.insertProductInfo(code, name, type, ccode, length,
                width, height, unit, maxstock, typename, CoverPage, contentpage, minstock)
                .compose(new CommonTransformer<InsertStatus>())
                .subscribe(new CommonSubscriber<InsertStatus>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(InsertStatus insertStatus) {
                        isInsert = true;
                        insertProductOnLinstener.insertPISuccess(insertStatus);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isInsert = false;
                        insertProductOnLinstener.insertPIFail(e);
                    }
                });

            return  isInsert;

    }

    public interface InsertProductOnLinstener {
        void insertPISuccess(InsertStatus insertStatus);

        void insertPIFail(Throwable throwable);
    }

}
